#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10, M = 2 * N;

int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[N];
bool st[N];
int n;

int gcd(int a, int b) // 欧几里得算法
{
    return b ? gcd(b, a % b) : a;
}

int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}

void add(int a, int b, int c) // 添加一条边a->b，边权为c
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int spfa() // 求1号点到n号点的最短路距离，如果从1号点无法走到n号点则返回-1
{
    int hh = 0, tt = 0;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q[tt++] = 1;
    st[1] = true;

    while (hh != tt)
    {
        int t = q[hh++];
        if (hh == N)
            hh = 0;
        st[t] = false;

        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if (!st[j]) // 如果队列中已存在j，则不需要将j重复插入
                {
                    q[tt++] = j;
                    if (tt == N)
                        tt = 0;
                    st[j] = true;
                }
            }
        }
    }

    if (dist[n] == 0x3f3f3f3f)
        return -1;
    return dist[n];
}

int main()
{
    n = 2021;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i++)
    {
        for (int j = max(1, i - 21); j <= min(n, i + 21); j++)
        {
            int t = lcm(i, j);
            add(i, j, t);
        }
    }

    cout << spfa() << endl;
    return 0;
}